#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& nums) {
        vector<vector<int>> ret;
        int n = nums.size();

        sort(nums.begin(), nums.end(), [&](const vector<int>& a, const vector<int>& b) {
            return a[0] < b[0];
            });
        int left = nums[0][0], right = nums[0][1];
        if (n == 1)return nums;
        for (int i = 1; i < n; i++) {
            int a = nums[i][0], b = nums[i][1];
            if (right >= a) {
                right = max(right, b);
            }
            else {
                ret.push_back({ left,right });
                left = a, right = b;
            }
        }
        ret.push_back({ left,right });
        return ret;
    }
};